home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / READF.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  13KB  |  387 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : READF.C
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *      This program reads the cubes from the input file, converts them to
  8.  *      minterms and then packs them in bits in the main memory.
  9.  *
  10.  *    Returns solution array if cube of all X's found
  11.  *              else an array of minterms
  12.  *
  13.  * --------------------------------------------------------------------------
  14.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  15.  *    University.
  16.  *
  17.  *    You are free to use, copy and distribute this software and its
  18.  *    documentation providing that:
  19.  *
  20.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  21.  *
  22.  *        IT IS NOT MODIFIED IN ANY WAY.
  23.  *
  24.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  25.  *
  26.  *    This program is provided "AS IS" without any warranty, expressed or
  27.  *    implied, including but not limited to fitness for any particular
  28.  *    purpose.
  29.  *
  30.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  31.  *    appreciated. Please send to:
  32.  *
  33.  *        Boon-Tiong Tan or Othman Bin Ahmad
  34.  *        School of EEE
  35.  *        Nanyang Technological University
  36.  *        Nanyang Avenue
  37.  *        Singapore 2263
  38.  *        Republic of Singapore
  39.  *
  40.  ***************************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <stdlib.h>
  45. #define mask8 255
  46.  
  47. unsigned char   *readf(fp, a, ondc)
  48. FILE            *fp;
  49. unsigned char   ondc, *a;
  50.  
  51. {
  52.    unsigned char   nspm, l, mterm, x, x_count, c, bptr=0, bit_num;
  53.    unsigned char   *temp, *hold, *nn, *cnn, *px, *cube, *p, *b;
  54.    unsigned long   m, k, mlast;
  55.    unsigned short  n, cu, i;
  56.    unsigned long   mem, terms, topow(), m_cnt, test, ccube;
  57.         char   e, j, exist();
  58.  
  59.  
  60.    /***
  61.     ***  READ NO. OF VARIABLES AND NO. OF CUBES FROM INPUT FILE
  62.     ***/
  63.  
  64.    if (ondc == 0)                             /* ON array */
  65.       {
  66.      nn = (unsigned char *) malloc(4);    /* space for no. of var */
  67.      if (nn==0)
  68.         {
  69.            printf("Out of Memory -- READF, *nn\n");
  70.            printf("Program terminated - 1\n");
  71.            exit(0);
  72.         }
  73.  
  74.      n = 0;
  75.      while (n == 0)                        /* take care of leading blank lines */
  76.         {
  77.            fgets(nn, 4, fp);              /* read no. of variable */
  78.            n = atoi(nn);                  /* ascii to integer */
  79.         }
  80.  
  81.      if (n > 255)                         /* exceed max limit */
  82.         {
  83.            printf("No. of variables exceeded maximum limit of 255 in input file.\n");
  84.            printf("Program terminated.\n");
  85.            exit(0);
  86.         }
  87.  
  88.      mlast = 0;                           /* reset no. of minterms */
  89.  
  90.      nspm = (n+7)/8;                      /* no. bytes/minterm */
  91.      mem  = 4 + nspm;                     /* amount to allocate */
  92.  
  93.      a = (unsigned char *) malloc(mem);   /* space for a -array */
  94.      if (a==0)
  95.         {
  96.            printf("Out of memory -- READF, *a\n");
  97.            printf("Progam terminated - 2\n");
  98.            exit(0);
  99.         }
  100.  
  101.      *a     = n;                          /* no. of variable */
  102.      *(a+3) = nspm;                       /* no. of storage per minterm */
  103.       }
  104.    else                                       /* DC array, ondc = 1 */
  105.       {
  106.      n  = *a;                             /* no. of variables */
  107.      nspm = *(a+3);                       /* no. of bytes/minterm */
  108.      mlast = *(a+1) | *(a+2)<<8;          /* no. of minterms from ON array */
  109.      mem = 4 + nspm*(1+mlast);            /* allocate in advance for next term */
  110.       }
  111.  
  112.    cnn = (unsigned char *) malloc(8);         /* space for no. of cubes */
  113.    if (cnn==0)
  114.       {
  115.      printf("Out of Memory -- READF, *cnn\n");
  116.      printf("Program terminated - 3\n");
  117.      exit(0);
  118.       }
  119.  
  120.    fgets(cnn, 8, fp);                         /* read no. of cubes */
  121.    cu = atoi(cnn);                            /* ascii to integer */
  122.  
  123.    /***
  124.     ***  BREAK THE CUBES INTO MINTERMS IF ANY
  125.     ***/
  126.  
  127.    if (cu != 0)                                   /* cube count not zero */
  128.       {
  129.      cube = (unsigned char *)malloc(n+12);    /* space for cube */
  130.      if (cube==0)
  131.         {
  132.            printf("Out of Memory -- READF, *cube\n");
  133.            printf("Program terminated - 4\n");
  134.            exit(0);
  135.         }
  136.      px = (unsigned char *)malloc(n+1);       /* space for position of X */
  137.      if (px==0)
  138.         {
  139.            printf("Out of Memory -- READF, *px\n");
  140.            printf("Program terminated - 5\n");
  141.            exit(0);
  142.         }
  143.      p = (unsigned char *)malloc(n+1);        /* space for p-array */
  144.      if (p==0)
  145.         {
  146.            printf("Out of Memory -- READF, *p\n");
  147.            printf("Program terminated - 6\n");
  148.            exit(0);
  149.         }
  150.      b = (unsigned char *)malloc(n+1);        /* space for b-array */
  151.      if (b==0)
  152.         {
  153.            printf("Out of Memory -- READF, *b\n");
  154.            printf("Program terminated - 7\n");
  155.            exit(0);
  156.         }
  157.      temp = (unsigned char *) malloc(nspm+1);   /* temporary store */
  158.      if (temp==0)
  159.         {
  160.            printf("Out of Memory -- READF, *temp\n");
  161.            printf("Program terminated - 8\n");
  162.            exit(0);
  163.         }
  164.       }
  165.  
  166.    m = mlast;                   /* no. of minterms */
  167.    i = 0;
  168.  
  169.    while (++i<=cu)              /* do for all cubes read */
  170.       {
  171.      x_count = 0;                      /* no. of X's in a cube */
  172.      fgets(cube, n+12, fp);            /* get cube from infile */
  173.      *(cube+n) = '\0';                 /* terminate with NULL string */
  174.  
  175.      x = 0;
  176.      for (j=n-1; j>=0; j--)            /* reverse the order */
  177.         {
  178.            if (*(cube+j) == 'x' || *(cube+j) == 'X')  /* X encountered */
  179.           {
  180.              *(px+x++) = n-j-1;        /* position of X's in cube */
  181.              *(p+n-j-1) = 1;           /* 1 in p-array indicates a X */
  182.              *(b+n-j-1) = 0;           /* position reset to zero */
  183.              x_count++;                /* no. of X's in a cube */
  184.           }
  185.            else                            /* a 0 or 1 encountered */
  186.           {
  187.              *(p+n-j-1) = 0;           /* 0 in p-array indicates a 0 or 1 */
  188.              *(b+n-j-1) = *(cube+j);   /* position assume input value */
  189.           }
  190.         }
  191.  
  192.      /***
  193.       ***   CUBE IS A MINTERM, JUST CONVERT IT INTO BINARY FORM, CHECK THAT IT HAS NOT
  194.       ***   EXISTED IN A-ARRAY BEFORE STORING IT IN A-ARRAY
  195.       ***/
  196.  
  197.      if (x_count == 0)                     /* it is a minterm */
  198.         {
  199.            c=0;                            /* a counter */
  200.  
  201.            if (n%8 == 0)                   /* multiple of bytes */
  202.           bit_num = 8;
  203.            else                            /* not multiple of bytes */
  204.           bit_num = (n%8);             /* remainder */
  205.  
  206.            for (j=0; j<nspm; j++)          /* if nspm>1 */
  207.           {
  208.              bptr = 0;                 /* reset bit pointer */
  209.              *(temp+j) = 0;                                     /* reset temporary storage */
  210.              while (bptr++<bit_num || (j!=(nspm-1) && bptr<=8)) /* bit storage operation */
  211.             {
  212.                ccube = *(cube+n-1-c++)-48;                  /* ascii to integer */
  213.                *(temp+j) = ccube<<(bptr-1) | *(temp+j);     /* temporary storage */
  214.             }
  215.           }
  216.  
  217.            e = exist(temp, a, m);          /* temp exist in A ? */
  218.            if (e == -1)                    /* don't exist */
  219.           {
  220.              memcpy((a+4+nspm*m), temp, nspm);       /* store in a-array */
  221.              m++;                                    /* no. of minterms */
  222.              mem = mem+nspm;                         /* space required */
  223.              a = (unsigned char *) realloc(a, mem);  /* space for a-array */
  224.              if (a==0)
  225.             {
  226.                printf("Out of Memory -- READF, *a\n");
  227.                printf("Program terminated - 9\n");
  228.                exit(0);
  229.             }
  230.           }
  231.         }
  232.  
  233.      /***
  234.       ***  IF CUBE CONSISTS OF X ONLY, RETURN THE SOLUTION OF ALL X OR ALL -
  235.       ***/
  236.  
  237.      else if (x_count == n)                  /* cube with all X's */
  238.            {
  239.           *a = n;                            /* no. of variables */
  240.           *(a+1) = 1;                        /* no. of product terms */
  241.           *(a+2) = 0;
  242.           *(a+3) = 0;                        /* indicate soln obtained */
  243.  
  244.           if (ondc==0 || (ondc==1 && mlast>0))    /* cube with all X's in ON array */
  245.          {
  246.             for (j=0; j<n; j++)
  247.                *(a+4+j) = 'X';                /* solution cube with all X's */
  248.             return (a);                       /* return solution array */
  249.          }
  250.           else                                    /* cube with all X's in DC array */
  251.          {
  252.             for (j=0; j<n; j++)
  253.                *(a+4+j) = '-';                /* solution cube with all - */
  254.             return (a);                       /* return solution array */
  255.          }
  256.            }
  257.     else
  258.        {
  259.            /***
  260.         ***  CUBE WITH SOME X, SO GENERATE THE 1ST MINTERM AND EXPAND TO
  261.         ***  GENERATE ALL OTHER MINTERMS COVER BY THE CUBE. CHECK THAT THEY
  262.         ***  ARE NOT ALREADY PRESENT IN A-ARRAY AND STORE THEM IN A-ARRAY
  263.         ***/
  264.  
  265.            l=0;                                   /* counters */
  266.            k=0;
  267.            mterm=0;                               /* temporary storage */
  268.  
  269.            /***
  270.         ***   GENERATE 1ST MINTERM
  271.         ***/
  272.  
  273.            for (j=0; j<n; j++)                    /* do for no. of var */
  274.           {
  275.              if (*(b+j) == '1')               /* a 1 in bit position */
  276.             mterm += topow(k);            /* convert to binary */
  277.              k++;
  278.              if (k==8 && ++l<=nspm)           /* new byte required */
  279.                 {
  280.                *(temp+l-1)=mterm;         /* store the completed byte */
  281.                k=0;                       /* reset counter */
  282.                mterm=0;                   /* reset for new byte */
  283.             }
  284.              if (k==n-8*l)                    /* less than 8-bit remains */
  285.             *(temp+l) = mterm;            /* transfer to temp */
  286.           }
  287.  
  288.            /***
  289.         ***  STORE 1ST MINTERM IN A-ARRAY IF NOT ALREADY EXISTED
  290.         ***/
  291.  
  292.            e = exist(temp, a, m);                          /* minterm exist ? */
  293.            if (e == -1)                                    /* don't exist */
  294.           {
  295.              memcpy((a+4+nspm*m), temp, nspm);         /* store in a-array */
  296.              m++;                                      /* no. of minterms */
  297.              mem = mem+nspm;                           /* space required */
  298.              a = (unsigned char *) realloc(a, mem);    /* more space */
  299.              if (a==0)
  300.             {
  301.                printf("Out of Memory -- READF, *a\n");
  302.                printf("Program terminated - 10\n");
  303.                exit(0);
  304.             }
  305.           }
  306.  
  307.         /***
  308.          ***  EXPAND USING THE 1ST MINTERM BASE ON THE NO. OF X AND THEIR POSITION
  309.          ***/
  310.  
  311.         hold = (unsigned char *) malloc(nspm+1);       /* temporary storage */
  312.         if (hold==0)
  313.             {
  314.                printf("Out of Memory -- READF, *hold\n");
  315.                printf("Program terminated - 11\n");
  316.                exit(0);
  317.             }
  318.  
  319.            m_cnt = 0;                  /* counter to simulate binary expansion */
  320.  
  321.            for (j=0; j<x_count; j++)   /* expand for the no. of X */
  322.           {
  323.              terms = topow(j);     /* no. of terms required in expansion */
  324.  
  325.              for (k=1; k<=terms; k++)           /* for all required terms */
  326.             {
  327.                m_cnt++;                     /* simulate binary expansion */
  328.                memcpy(hold, temp, nspm);    /* transfer 1st minterm */
  329.  
  330.                for (l=0; l<=j; l++)         /* no. of times = pos of X */
  331.                    {
  332.                  test = m_cnt & (1<<l);     /* addition required ? */
  333.                  if (test != 0)
  334.                     *(hold + *(px+l)/8) = *(hold + *(px+l)/8)  /* add */
  335.                               + topow(*(px+l)%8);
  336.                   }
  337.  
  338.                /***
  339.                 ***  STORE THE GENERATED MINTERM IF NOT ALREADY EXISTED IN A-ARRAY
  340.                 ***/
  341.  
  342.                e = exist(hold, a, m);      /* minterm exist in a-array ? */
  343.  
  344.                if (e == -1)                /* don't exist */
  345.                   {
  346.                  memcpy((a+4+nspm*m), hold, nspm);       /* store in a-array */
  347.                  m++;                                    /* no. of minterms */
  348.                  mem = mem+nspm;                         /* space required */
  349.                  a = (unsigned char *) realloc(a, mem);  /* more space */
  350.                  if (a==0)
  351.                     {
  352.                        printf("Out of Memory -- READF, *a\n");
  353.                        printf("Program terminated - 12\n");
  354.                        exit(0);
  355.                     }
  356.                   }
  357.             }
  358.           }
  359.            free(hold);          /* free pointer */
  360.         }
  361.       }
  362.  
  363.    if (ondc == 0)                   /* ON-array processed */
  364.       free(nn);
  365.    free(cnn);
  366.    if (cu != 0)                     /* cube(s) processed */
  367.       {
  368.      free(cube);                /* free pointers used */
  369.      free(px);
  370.      free(b);
  371.      free(p);
  372.      free(temp);
  373.       }
  374.  
  375.    if (m > 65535)                   /* max no. of minterms exceeded */
  376.       {
  377.      printf("Maximum number of minterms exceeded !\n");
  378.      printf("Program terminated.\n");
  379.      exit(0);
  380.       }
  381.  
  382.    *(a+1) = m & mask8;              /* no. of minterms in 2 bytes */
  383.    *(a+2) = m >> 8;
  384.  
  385.    return(a);                       /* return array of minterms */
  386. }
  387.